/*
Created by Vadim Shchemelinin aka badger 
2012 St.Petersburg

Friz Time on start. Init.

[_time,[_zone1,_zone2,...],_safeDist] call bgr_fnc_frizTime;
*/

_time = [_this,0,1,[0]] call BIS_fnc_param;
_zones = [_this,1,[],[[]]] call BIS_fnc_param;
_safeDistance = [_this,2,50,[0]] call BIS_fnc_param;

////////////////////////////////
////        Server        //////
////////////////////////////////
if (isServer) then {	
	0 = [_time] spawn {				
		_briefingTime = _this select 0;
				
		bgr_frizTime_Wready = 0;
		bgr_frizTime_Eready = 0;
		bgr_frizTime_Rready = 0;
		bgr_frizTime_Cready = 0;
			
		//TODO if no player on side, then make side ready
		_flags = [1,1,1,1];
		{
			if(isPlayer _x) then {
				switch (side _x) do {
					case west: {_flags set [0, 0];};
					case east: {_flags set [1, 0];};
					case resistance: {_flags set [2, 0];};
					case civilian: {_flags set [3, 0];};
				};
			};		
		}foreach allUnits;
		bgr_frizTime_Wready = _flags select 0;
		bgr_frizTime_Eready = _flags select 1;
		bgr_frizTime_Rready = _flags select 2;
		bgr_frizTime_Cready = _flags select 3;
		publicvariable "bgr_frizTime_Wready";
		publicvariable "bgr_frizTime_Eready";
		publicvariable "bgr_frizTime_Rready";
		publicvariable "bgr_frizTime_Cready";

		bgr_frizTime_START = false;
		publicvariable "bgr_frizTime_START";

		waitUntil{(time>0)};
		0 = _briefingTime spawn {
			waitUntil{
				_text = format ["%1 minutes remaining",_this];
				[[[_text],{hint (_this select 0)}],"BIS_fnc_spawn"] spawn BIS_fnc_MP;
				_waitTime = diag_tickTime + 60;
				waitUntil{sleep 5;diag_tickTime>_waitTime};
				_this = _this - 1;
				(_this<1)||(bgr_frizTime_START)
			};
		bgr_frizTime_START = true;
		};
		waitUntil{
			sleep 1;
			(bgr_frizTime_Wready+bgr_frizTime_Eready+bgr_frizTime_Rready+bgr_frizTime_Cready == 4)||(bgr_frizTime_START)
		};

		bgr_frizTime_START = true;
		publicvariable "bgr_frizTime_START";
	};
};

////////////////////////////////
////        Client        //////
////////////////////////////////
if (!isDedicated) then {
	0 = [[_safeDistance],_zones] spawn {
		_settings = _this select 0;
		_safeDistance = _settings select 0;
		 
		_zones = [];
		_markerSafeZone = false;
		_timeout = 0.2;
		if (count (_this select 1) > 0) then {
			_zones = _this select 1;
			{_x setMarkerAlphaLocal 0} foreach _zones;
			_markerSafeZone = true;
		};

		//////////////////////////////////
		waitUntil{(!isNil("bgr_frizTime_START"))and(time>0)};
		//////////////////////////////////
		_startPos = getPosATL vehicle player;
		//lower players weapon
		if (vehicle player == player) then {
			player switchMove "AmovPercMstpSlowWrflDnon";
		};

		//////////////////////////////////////////////////
		//======= player move control ========
		0 = [player,_zones,_markerSafeZone,_startPos,"bgr_frizTime_START",_timeout,_safeDistance,true] spawn  bgr_fnc_allowZone;

		//////////////////////////////////////////////////
		//======= player fire control ========
		player addEventHandler ["Fired", {
			if (!bgr_frizTime_START) then {
				if !((_this select 5) isKindOf "SmokeShell") then {
					deleteVehicle (_this select 6);
					hintC "Do not shoot at brifing!";
				};
			};
		}];
		/*
		//waitUntil{!isNil("ace_sys_weaponselect_fnc_keypressed")};
		//[0,-1] call ace_sys_weaponselect_fnc_keypressed;
		_not_shot = {hint 'No!';};
		_blocker = (findDisplay 46) displayAddEventHandler ["MouseButtonDown", '
			[] call _not_shot;
			false
		'];
		*/

		//////////////////////////////////////////////////
		//======= player vehicle control ========
		0 = [] spawn {
			waitUntil {
				sleep 0.1;
				if ((vehicle player != player)and(driver vehicle player == player)) then {
					vehicle player setVelocity [0, 0, 0];
				};
				bgr_frizTime_START
			};
		};

		//////////////////////////////////
		//======= LEADER ========
		_bgr_friz_clnt_trigger = [] call bgr_fnc_frizTriggerCreate;

		//////////////////////////////////
		//======= wait ========
		waitUntil {
				sleep 0.5;
				bgr_frizTime_START
		};
		//==================================
		//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\



		//////////////////////////////////
		/*
		(findDisplay 46) displayRemoveEventHandler ["MouseButtonDown",_blocker];
		//[0,0] call ace_sys_weaponselect_fnc_keypressed;
		*/

		//////////////////////////////////
		deleteVehicle _bgr_friz_clnt_trigger;

		//////////////////////////////////
		//======= message about start ========
		["TaskCreated",['',"Start the Game! Go! Go! Go!"]] call BIS_fnc_showNotification;
	};
};
true